from numpy import *
import matplotlib.pyplot as plt
import glob
import pylab as P

def getAttemptInfo(dirname):
    filename = glob.glob(dirname + '/*stepcnt*')[0]
    fitness = float(filename.split('_')[1].split('\\')[-1])
    time = int(filename.split('_time=')[1].split('_')[0])
    steps = int(filename.split('_stepcnt=')[1])
    return [fitness, time, steps]

def countNumberOfAttemptsForStepInterval(From, To):
    result = 0
    for st in steps:
        if From <= st <= To:
            result += 1
    return result

attemptsInfo = []
for dirname in glob.glob('attempts_clockAcoOnly/attempt*'):
    attemptsInfo += [getAttemptInfo(dirname)]

attemptsInfo = sorted(attemptsInfo)
fitnesses = array([v[0] for v in attemptsInfo])
time = array([v[1] for v in attemptsInfo])
steps = array([v[2] for v in attemptsInfo])


s = arange(steps.min(), steps.max() + 0.1, (steps.max() - steps.min()) / 100)
oc = zeros(len(s))

for i in range(len(s) - 1):
    oc[i] = 100 * countNumberOfAttemptsForStepInterval(s[i], s[i + 1]) / float(len(steps))


"""
ex = sum(steps) / len(steps)
sigma = 0.0
for x in steps:
    sigma += (x - ex) * (x - ex)
sigma = sqrt(sigma / (len(steps) - 1))

print "E(X) = ", ex 
print "sigma(X) = ", sigma
#exit()"""

plt.ylabel('Percentage of built automatons')
plt.xlabel('Number of steps for building')
plt.bar(s, oc, 10000)
#plt.subplot(212)
#plt.plot(s, oc)

#n, bins, patches = P.hist(steps, 100, normed=False, histtype='bar')
#P.setp(patches, 'facecolor', 'g', 'alpha', 1)
#print sum(n * diff(bins))


print 'For building best automaton:'
print 'minTime = ', time.min(), '; minSteps = ', steps.min()
print 'maxTime = ', time.max(), '; maxSteps = ', steps.max()
print 'averageTime = ', sum(time) / len(time), '; averageSteps = ', sum(steps) / len(steps)
plt.show()







    

